#include <iostream>

#define MAX 1000
using namespace std;

//设计联系人的结构体
struct Person {
    string name;//姓名
    string sex;//性别
    int age;//年龄
    long long phone;//电话号吗
    string address;//坚挺住址
};
//设计通讯录的结构体
struct AddressBooks {
    struct Person array[MAX];//通讯录中保存的联系人的数组
    int size;//通讯录中当前记录的联系人个数
};

//1、显示菜单界面
void showMenu() {
    cout << "***********************" << endl;
    cout << "***** 1、添加联系人 *****" << endl;
    cout << "***** 2、显示联系人 *****" << endl;
    cout << "***** 3、删除联系人 *****" << endl;
    cout << "***** 4、查找联系人 *****" << endl;
    cout << "***** 5、修改联系人 *****" << endl;
    cout << "***** 6、清空联系人 *****" << endl;
    cout << "***** 0、退出通讯录 *****" << endl;
    cout << "***********************" << endl;
}

//2、添加联系人
void addPerson(AddressBooks *books) {
    if (books->size == MAX) {
        cout << "通讯录已经满了，无法继续添加!" << endl;
        return;
    } else {
        //添加具体的联系人
        //姓名
        cout << "请输入姓名:";
        cin >> books->array[books->size].name;
        //性别
        while (true) {
            int num;
            cout << "请输入性别:(1-男，2-女)";
            cin >> num;
            if (num == 1) {
                books->array[books->size].sex = "男";
                break;
            } else if (num == 2) {
                books->array[books->size].sex = "女";
                break;
            } else {
                cout << "没有这个选项，请重新输入" << endl;
                break;
            }
        }
        //年龄
        while (true) {
            int age;
            cout << "请输入年龄:";
            cin >> age;
            if (age > 0 && age < 100) {
                books->array[books->size].age = age;
                break;
            } else {
                cout << "数据有误，请重新输入" << endl;
                break;
            }
        }
        //电话
        cout << "请输入电话:";
        cin >> books->array[books->size].phone;
        //住址
        cout << "请输入家庭住址:";
        cin >> books->array[books->size].address;
    }
    //更新通讯录人数
    books->size++;
    cout << "添加成功" << endl;
}

//3、显示联系人
void showPerson(const AddressBooks *books) {
    //判断通讯录中人数是否为0，如果为0，提示记录为空
    //如果不为0，显示记录的联系人信息
    if (books->size == 0) {
        cout << "联系人记录为空" << endl;
    } else {
        for (int i = 0; i < books->size; ++i) {
            cout << "姓名:" << books->array[i].name << "\t";
            cout << "性别:" << books->array[i].sex << "\t";
            cout << "年龄:" << books->array[i].age << "\t";
            cout << "电话:" << books->array[i].phone << "\t";
            cout << "地址:" << books->array[i].address << endl;
        }
    }
}

//4、判断联系人是否存在
//检测联系人是否存在，如果存在，返回联系人所在数组中的具体位置，不存在返回-1
int containPerson(const AddressBooks *books, string name) {
    for (int i = 0; i < books->size; ++i) {
        if (books->array[i].name == name) {
            return i;//找到了，返回这个人在数组中的下标编号
        }
    }
    return -1;//如果遍历结束都没有找到，返回-1
}

//5、删除联系人
void deletePerson(AddressBooks *books) {
    string name;
    cout << "请输入您需要删除的联系人:";
    cin >> name;
    int ret = containPerson(books, name);
    if (ret == -1) {
        cout << "系统中没有" << name << "这个人" << endl;
    } else {
        for (int i = ret; i < books->size; i++) {
            books->array[i] = books->array[i + 1];
        }
        books->size--;
        cout << "删除成功" << endl;
    }
}

//6、查找联系人
void findPerson(const AddressBooks *books) {
    string name;
    cout << "请输入您需要查找的联系人:";
    cin >> name;
    int ret = containPerson(books, name);
    if (ret == -1) {
        cout << "系统中没有" << name << "这个人" << endl;
    } else {
        cout << "姓名:" << books->array[ret].name << "\t";
        cout << "性别:" << books->array[ret].sex << "\t";
        cout << "年龄:" << books->array[ret].age << "\t";
        cout << "电话:" << books->array[ret].phone << "\t";
        cout << "地址:" << books->array[ret].address << endl;
    }
}

//7、修改联系人
void modifyPerson(AddressBooks *books) {
    string name;
    cout << "请输入您需要修改的联系人:";
    cin >> name;
    int ret = containPerson(books, name);
    if (ret == -1) {
        cout << "系统中没有" << name << "这个人" << endl;
    } else {
        cout << "姓名:" << books->array[ret].name << "\t";
        cout << "性别:" << books->array[ret].sex << "\t";
        cout << "年龄:" << books->array[ret].age << "\t";
        cout << "电话:" << books->array[ret].phone << "\t";
        cout << "地址:" << books->array[ret].address << endl;
        int num;
        cout << "请输入你需要修改的信息:(1-姓名，2-性别，3-年龄，4-电话，5-地址)";
        cin >> num;
        switch (num) {
            case 1: {
                cout << "请输入姓名:";
                cin >> books->array[ret].name;
                break;
            }
            case 2: {
                while (true) {
                    int num;
                    cout << "请输入性别:(1-男，2-女)";
                    cin >> num;
                    if (num == 1) {
                        books->array[ret].sex = "男";
                        break;
                    } else if (num == 2) {
                        books->array[ret].sex = "女";
                        break;
                    } else {
                        cout << "没有这个选项，请重新输入" << endl;
                        break;
                    }
                }
                break;
            }
            case 3: {
                while (true) {
                    int age;
                    cout << "请输入年龄:";
                    cin >> age;
                    if (age > 0 && age < 100) {
                        books->array[ret].age = age;
                        break;
                    } else {
                        cout << "数据有误，请重新输入" << endl;
                        break;
                    }
                }
                break;
            }
            case 4: {
                cout << "请输入电话:";
                cin >> books->array[ret].phone;
                break;
            }
            case 5: {
                cout << "请输入家庭住址:";
                cin >> books->array[ret].address;
                break;
            }
            default: {
                cout << "没有这个选项，请重新输入" << endl;
                break;
            }
        }
        cout << "修改成功" << endl;
    }
}

//8、清空联系人
void cleanPerson(AddressBooks *books) {
    char ch;
    cout << "你确定要清空通讯录吗??" << endl;
    cout << "请输入你的选择(y/n):";
    cin >> ch;
    if (ch == 'y' || ch == 'Y') {
        books->size = 0;
        cout << "通讯录已清空!" << endl;
    } else {
        cout << "清空操作终止" << endl;
    }
}